package daemon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Arrays;

import common.*;

/**
 * Primary class target is to dispose commands to be handled by storage class.
 * Commands can be passed to longcat either by arguments given in main function
 * or by command line implemented inside main function.
 */
public class LongcatDaemon{

	/**
	 * A path to longcat dedicated directory.
	 * Longcat dedicated directory gathers all packages which belong to the cloud. 
	 * No other files are stored here. 
	 * All packages are anonymous and longcat server is the only host which has the information about 
	 * exact packages' locations. 
	 */
	private static String packagePath;
	
	//TODO: comment
	private static void initializeRMI(Storage storage) throws RemoteException {
		Registry registry = LocateRegistry.createRegistry(0);
		try {
			registry.bind("StorageInterface", storage);
		} catch(AlreadyBoundException e) { assert(false); }
	}
	
	/**
	 * 
	 * Main function handles instructions given function's arguments and proceeds to 
	 * processing command line instructions. 
	 * @param args First argument is a path to longcat dedicated directory. 
	 * Other arguments are optional and describe command to be handled at once.
	 * "s" - store, "g" - get.
	 * 
	 * Commands available for user are as following:<br>
	 * <UL>
	 * <LI>list - Lists all files available in the distributed directory.
	 * <LI>get - Downloads specified file from a directory.
	 * <LI>store - Stores local file on the distributed directory
	 * <LI>delete - Deletes file from the distributed directory.
	 * </UL>
	 * 
	 * Moreover two additional functionalities are implemented, but are not available from command line<br>
	 * <UL>
	 * <LI>hello, run in order to register particular client on the remote directory. 
	 * <LI>goodbye, run to unregister a client. 
	 * </UL>
	 * Further notes on this functions are available here: @see {@link Storage#disconnect()}
	 */
	public static void main(String[] args) throws NotBoundException, IOException {
		LongcatServerInterface server;
		if(args.length != 3) {
			System.out.println("Wrong number of args");
			return;
		}
		packagePath = args[0];
		String host = args[1];
		int port = Integer.parseInt(args[2]);
		server = (LongcatServerInterface) Naming.lookup("//" + host + ":" + port + "/LongcatServerInterface");
		
		
		Storage storage = new Storage(server, packagePath, 0);
		initializeRMI(storage);
		
		 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		    String commandLine;
		    String delims = "\"";
		    
	    while ((commandLine = stdin.readLine()) != null && commandLine.length() != 0){
	    	String myArgs[] = commandLine.split(delims);
	    	for(int i=0; i<myArgs.length; i++)
	    		myArgs[i] = myArgs[i].trim();
	    	if(myArgs.length==1){
	    		if(myArgs[0].equals("list")){
	    			System.out.println("listujemy");
	    		}else if(myArgs[0].equals("disconnect")){
	    			System.out.println("disconnectujemy");
	    		}else{
		    		System.out.println("SYNTAX ERROR");
	    		}
	    	}else if(myArgs.length==4){
		    	if(myArgs[0].equals("get")){
		    		storage.get(myArgs[1],myArgs[3]);
		    		System.out.println("gettujemy z parametrami: ("+myArgs[1]+", "+myArgs[3]+").");
		    	}else if(myArgs[0].equals("store")){
		    		try {
						storage.store(myArgs[1], myArgs[3]);
					} catch (Exception e) {
						e.printStackTrace();
					}
		    		System.out.println("storujemy z parametrami: ("+myArgs[1]+", "+myArgs[3]+").");
		    	}else{
		    		System.out.println("SYNTAX ERROR");
		    	}
	    	}else{
	    		System.out.println("SYNTAX ERROR");
	    	}
	    }
		
	}  

}
